var express = require('express');
var md5=require('md5');
const User=require('../models/user_model');
const Comment=require('../models/comment_model');
const Url= require("../models/url.js");
const Song= require("../models/details.js");
const Mvs = require("../models/mvs.js")
var router = express.Router();
const KEY='zFSD13a2sd16SD';
router.use(function(req,res,next){
    next()
})
/* GET home page. */
router.get('/', async function(req, res) {
    res.send('/')
});

router.get('/login',async function(req, res) {
    let user=await User.findOne({username:req.query.username})
    if(user){
        if(user.password==md5(req.query.password+KEY)){
            res.json({
                code:200,
                msg:'密码正确'
            })
        }else{
            res.json({
                code:201,
                msg:'密码不正确'
            })
        }
    }else{
        res.json({
            code:202,
            msg:'用户不存在'
        })
    }
});
router.get('/register',async function (req,res) {
    let u=await User.findOne({username:req.query.username})
    if(u){
        res.json({
            code:201,
            msg:'用户名已存在',
            data:[]
        })
    }else{
        await User.create({username:req.query.username,password:req.query.password})
        let flag=await User.findOne({username:req.query.username})
        res.json({
            code:200,
            msg:'注册成功',
            data:flag
        })
    }
});
router.get('/comment/hot',async function(req, res) {
    let c=await Comment.find({id:req.query.id})
    if(c){
        res.json({
            code:200,
            msg:'success',
            data:c
        })
    }else{
        res.json({
            code:201,
            msg:'success',
            data:[]
        })
    }
});
router.get('/search', function(req, res, next) {
    var last=[];
    var user = Song.find({}).find((err,result)=>{
        last=result;
        last=last.filter((item)=>{
            var flag=item.name.indexOf(decodeURIComponent(req.query.keywords))>=0 ? true:item.ar.name.indexOf(decodeURIComponent(req.query.keywords))>=0 ? true:false
            return flag
        })
        res.send({
            status:'success',
            msg:'搜索歌曲信息查询成功！',
            data:last
        })
    })
});
router.get('/song/url', function(req, res, next) {
    var last=[];
    var user = Url.find({id:req.query.id}).find((err,result)=>{
        last=result;
        res.send({
            status:'success',
            msg:'歌曲url查询成功！',
            data:last
        })
    })
});
router.get('/getsongs',function (req,res) {
    let arr=[]
    for(let i=0;i<u.length;i++){
        arr.push({id:u[i].data.data[0].id,url:u[i].data.data[0].url})
    }
    res.json(arr)
});  //暂时没用
router.get('/set',function (req,res) {
    let last=[];
    Song.createIndex({'mvid':0})
    let s=Song.find({}).find((err,result)=>{
        last=result
        last=last.filter((item)=>{
            return true;
        })
        res.send('set')
    })
}) //暂时没用

//获取歌曲详情
router.get('/song/detail',function (req,res,next){
    var last=[];
    console.log(req.query.id)
    var songs = Song.find({id:req.query.id}).find((err,result)=>{
        last = result;
        res.send({
            status:'success',
            msg:'歌曲详情查询成功！',
            data:last
        })
        console.log(last)
    })
})

//获取歌曲mv
router.get('/mv/url', function (req,res,next) {
    var last = [];
    console.log(req.query.id)
    var mvs = Mvs.find({id:req.query.id}).find((err,result)=>{
        last = result;
        res.send({
            status:'success',
            msg:'歌曲 mv 查询成功！',
            data:last
        })
    })
})

module.exports = router;